package 顺序表;

import java.util.Arrays;

public class MyArrayList implements MyList{
    private Integer[] array;
    private int size;

    public MyArrayList(){
        array = new Integer[16];
        size = 0;
    }

    // 供内部扩容
    private void ensureCapacity(){
        if (size < array.length){
            return;
        }

        // 进行扩容
        // 1. 找到一个新的房子，一般来说，房子大小按 2 倍进行扩容
        Integer[] newArray = new Integer[array.length * 2];

        // 2.把原来房子中的东西全部搬到新房子中
        for (int i = 0; i < array.length; i++){
            newArray[i] = array[i];
        }

        // 3. 公布新房子的位置
        this.array = newArray;
    }

    @Override
    public boolean add(Integer e) {
        ensureCapacity();
        array[size] = e;
        size++;
        return true;
    }

    @Override
    public void add(int index, Integer e) {
        ensureCapacity();
        if (index < 0 || index > size){
            throw new ArrayIndexOutOfBoundsException("Index:" + index + ", Size:" + size);
        }

        for (int to = size + 1; to > index; to--){
            array[to] = array[to - 1];
        }

        array[index] = e;
        size++;
    }

    @Override
    public Integer remove(int index) {
        if (index < 0 || index >= size){
            throw new ArrayIndexOutOfBoundsException("Index:" + index + ", Size:" + size);
        }
        Integer e = array[index];
        for (int to = index; to < size - 1; to++){
            array[to] = array[to + 1];
        }
        array[size - 1] = null;
        size--;
        return e;
    }

    @Override
    public boolean remove(Integer e) {
        int i = indexOf(e);
        if (i < 0){
            return false;
        }
        remove(i);
        return true;
    }

    @Override
    public Integer get(int index) {
        if (index < 0 || index >= size){
            throw new ArrayIndexOutOfBoundsException("Index:" + index + ", Size:" + size);
        }
        return array[index];
    }

    @Override
    public Integer set(int index, Integer e) {
        if (index < 0 || index >= size){
            throw new ArrayIndexOutOfBoundsException("Index:" + index + ", Size:" + size);
        }
        Integer oldElement = array[index];
        array[index] = e;
        return oldElement;
    }

    @Override
    public boolean contains(Integer e) {
        if (indexOf(e) >= 0){
            return true;
        }
        return false;
    }

    @Override
    public int indexOf(Integer e) {
        for (int i = 0; i < size; i++){
            if (e.equals(array[i])){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int lastIndexOf(Integer e) {
        for (int i = size - 1; i >= 0; i--){
            if (e.equals(array[i])){
                return i;
            }
        }
        return -1;
    }

    @Override
    public void clear() {
        Arrays.fill(array, null);
        size = 0;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public String toString() {
        Integer[] toShow = Arrays.copyOf(array, size);
        return Arrays.toString(toShow);
    }
}
